﻿// For an introduction to the Navigation template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232506
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var nav = WinJS.Navigation;
    var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
    var as = Windows.UI.ApplicationSettings;
    var settingsPane = as.SettingsPane.getForCurrentView();
    var roamingSettings = Windows.Storage.ApplicationData.current.roamingSettings;

    WinJS.Application.onsettings = function (e) {
        e.detail.applicationcommands = { "ChangeTheme": { title: "Change theme", href: "pages/changetheme/changetheme.html" } };

        WinJS.UI.SettingsFlyout.populateSettings(e);
    };

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // share event
                var shareFileHandler = function (event) {
                    var dataRequest = event.request;
                    var selected = document.getElementsByClassName("win-selected")[0];
                    if (selected) {
                        dataRequest.data.properties.title = "New file for sharing";
                        dataRequest.data.properties.description = "Sound file from our application";

                        dataRequest.data.properties.fileTypes.replaceAll([".wav"]);

                        var deferral = dataRequest.getDeferral();

                        var keyBind = document.getElementsByClassName("win-selected")[0].innerText.match("[^\r\n]+")[0];
                        var object = ViewModels.getInstanceByKeyBind(keyBind);

                        var uri = new Windows.Foundation.Uri("ms-appx://" + object.soundSource);
                        var file = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (result) {
                            dataRequest.data.setStorageItems([result]);
                            deferral.complete();
                        }, function (error) {
                            deferral.complete();
                        }).done(null, function (err) {
                            deferral.complete();
                        });
                    }
                }

                dataTransferManager.addEventListener("datarequested", shareFileHandler);
                //settings panel
                settingsPane.addEventListener("commandsrequested", onCommandsRequested);

                var onCommandsRequested = function (e) {
                    var commandHelp = new as.SettingsCommand("privacy", "Privacy Policy", privacyCommandInvoked);
                    e.request.applicationCommands.append(commandHelp);
                }

                var privacyCommandInvoked = function (e) {
                    var uri = new Windows.Foundation.Uri("http://www.kraigbrockschmidt.com/src/HereMyAm_PrivacyStatement.html");
                    Windows.System.Launcher.launchUriAsync(uri).done();
                }

                //theme saver
                var lightTheme = document.getElementById("light-theme");
                var darkTheme = document.getElementById("dark-theme");

                if (roamingSettings.values['theme'] == "dark") {
                    lightTheme.disabled = true;
                    darkTheme.disabled = false;
                }
                else {
                    lightTheme.disabled = false;
                    darkTheme.disabled = true;
                }
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }

            if (app.sessionState.history) {
                nav.history = app.sessionState.history;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {
                if (nav.location) {
                    nav.history.current.initialPlaceholder = true;
                    return nav.navigate(nav.location, nav.state);
                } else {
                    return nav.navigate(Application.navigator.home);
                }
            }));
        }
    });

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. If you need to 
        // complete an asynchronous operation before your application is 
        // suspended, call args.setPromise().
        app.sessionState.history = nav.history;
    };

    app.start();
})();
